/**
 * Created by Webstorm.
 * @desc 本文件骨架由系统自动生成，开发者只需要在对应的模块中定义指定的state或者action，随后即可在pages中使用
 */
import { GET_PRODUCT_INFO, ORDER_INFO } from '../mutation-types'
import api from 'api'
import {doAction} from '../util'

// state定义
const state = {
  product: {},
  orderInfo: {}
}

// mutations定义
const mutations = {
  [GET_PRODUCT_INFO](state, payload) {
    if (payload.title && payload.title.length > 38) {
      payload.resetTitle = payload.title.substr(0, 38) + '...';
    }
    state.product = payload || {};
    state.orderInfo = payload || {};
  },
  [ORDER_INFO](state, payload) {
    const orderInfo = { ...state.orderInfo, ...payload };
    state.orderInfo = orderInfo || {}
    return state.orderInfo
  }
}

// actions定义
const actions = {
  getProductInfo(store = {}, params) {
    return doAction({
      api: api.getProductInfo,
      params,
      mutationName: GET_PRODUCT_INFO
    }).catch(() => {
      console.log('接口错误');
    })
  },
  setOrderInfo ({ commit, state }, payload) {
    return new Promise((resolve) => {
      commit('ORDER_INFO', payload)
      resolve()
    })
  }
}

export default {
  state,
  mutations,
  actions,
}